package com.jz.opengl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.IdRes;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author jackzhous
 * @package com.jz.opengl
 * @filename RawHelper
 * date on 2019/6/27 2:29 PM
 * @describe
 * raw文件夹读取资源文件
 * @email jackzhouyu@foxmail.com
 **/
public class RawHelper {


    /**
     * 读取Raw资源文件
     * @param context
     * @param resId
     * @return
     */
    @SuppressLint("ResourceType")
    public static String readRawFile(Context context, int resId){
        if(context == null){
            return null;
        }

        InputStream stream = null;
        BufferedReader reader = null;
        InputStreamReader inputStreamReader = null;
        StringBuilder sb = new StringBuilder();


        try {
            stream = context.getResources().openRawResource(resId);
            inputStreamReader = new InputStreamReader(stream);
            reader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = reader.readLine()) != null){
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(stream != null){
                    stream.close();
                }
                if(inputStreamReader != null){
                    inputStreamReader.close();
                }
                if(reader != null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();

    }

}
